package com.tsx.leetcode.hot100.easy;

import com.tsx.leetcode.hot100.common.TreeNode;

/**
 * <a href="https://leetcode.cn/problems/merge-two-binary-trees">合并二叉树</a>
 */
public class Q617MergeTrees {
    public static void main(String[] args) {

    }

    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 == null || root2 == null) {
            return root1 == null ? root2 : root1;
        }
        return dfs(root1, root2);
    }

    public TreeNode dfs(TreeNode r1, TreeNode r2) {
        if (r1 == null || r2 == null) {
            return r1 == null ? r2 : r1;
        }
        r1.val = r1.val + r2.val;
        r1.left = dfs(r1.left, r2.left);
        r1.right = dfs(r1.right, r2.right);
        return r1;
    }
}
